# 模糊隶属度计算
import cv2
import custom

pic = cv2.imread("pic/a1.png");
height, width, channel = pic.shape  # 查看图片大小
# print(height,width,channel)

hist = cv2.calcHist([pic], [0], None, [256], [0, 256]) #仅计算了一层的频率，共有三层BGR
# 查看灰度值频率
# for i in hist:
#     print(i);

# 求灰度值概率
p = []
for i in hist:
    p.append(i / (height * width))
# print(p) #输出概率

# custom.showPicture("test",pic);
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# 一阶模糊矩
m = 0
for i in range(256 - 1):
    m += (i / 255) * p[i];
print("一阶模糊矩为：", m)

#三成图像
u_dark, u_bright = [[],[],[]], [[],[],[]]
a = 0.4
l = (1 - 2 * a) / (a * a)
for k in range(0, 3):
    for i in range(0, height):
        for j in range(0, width):
            pic_now = pic[i][j][k]/255
            if pic_now < a:
                u_dark[k].append(1 / a * (pic_now ** 2))
            else:
                u_bright[k].append((1 - (1 / a * (((1 - pic_now) / (1 + l * pic_now)) ** 2))) / (1 + l * (1 / a * (((1 - pic_now) / (1 + l * pic_now)) ** 2))))

u_bright_max, u_dark_max = [], []
for k in range(0, 3):
    u_bright_max.append(max(u_bright[k]))
    u_dark_max.append(max(u_dark[k]))
    print(u_bright_max[k], u_dark_max[k])
